<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: staff_categories.php,v $ : $Revision: 1.12 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["kb_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./modules/knowledgebase/functions_knowledgebase.php");
require_once ("./includes/functions_html.php");
require_once ("./includes/functions_template.php");
require_once ("./includes/functions_jscache.php");
require_once ("./includes/data_mimelist.php");
$template->loadLanguageSection("knowledgebase");

$interface->assignQuickLink($_SWIFT["language"]["managecategories"], "index.php?_m=knowledgebase&_a=managecategories", "icon_list.gif");
$interface->assignQuickLink($_SWIFT["language"]["managequestions"], "index.php?_m=knowledgebase&_a=managequestions", "icon_topicyellow.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertcategory"], "index.php?_m=knowledgebase&_a=insertcategory", "icon_newcategory.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertquestion"], "index.php?_m=knowledgebase&_a=insertquestion", "icon_inserttopic.gif");

renderCategoryList();

/**
* ###############################################
* MANAGE KNOWLEDGEBASE CATEGORIES
* ###############################################
*/
if ($eventaction == "managecategories")
{
	if ($_GET["do"] == "delete" && !empty($_GET["kbcategoryid"]))
	{
		$totaldel = deleteKnowledgebaseCategory($_GET["kbcategoryid"]);

		printRedirect(sprintf($_SWIFT["language"]["catdeleteconfirm"], htmlspecialchars($totaldel)), "index.php?_m=knowledgebase&_a=managecategories&catdel=".urlencode($totaldel));
		exit;
	}

	if (!empty($_REQUEST["insertcat"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["kbcatinsertconfirm"], htmlspecialchars($_REQUEST["insertcat"]));
	} else if (!empty($_REQUEST["catdel"])) {
		$infomessage = sprintf($_SWIFT["language"]["catdeleteconfirm"], htmlspecialchars($_REQUEST["catdel"]));
	} else if (!empty($_REQUEST["updatecat"])) {
		$infomessage = sprintf($_SWIFT["language"]["kbcatupdateconfirm"], htmlspecialchars($_REQUEST["updatecat"]));
	}

	require_once ("./includes/TreeMenu/TreeMenu.php");
	$template->assign("treemenu", true);

	$icon = 'icon_folderyellow3.gif';
	$expandedIcon = 'icon_folderyellow3.gif';

	$menu = new HTML_TreeMenu();

	// Do we need to filter it to one user group?
	$dofilter = false;
	if ($_POST["filter"] == "1")
	{
		$dofilter = true;
	}

	// Is it filtered result?
	if ($dofilter == true)
	{
		// Build the entire knowledgebase category data with filtering enabled
		$result = buildKBCategoryData($_POST["filtertgroupid"]);
	} else {
		// Build the knowledgebase category data WITHOUT the filtering enabled
		$result = buildKBCategoryData(false);
	}
	$categorydata = $result["categorydata"];
	$categoryidlist = $result["categoryidlist"];

	// Now we build up the nodes for this..
	$menu  = new HTML_TreeMenu();

	$node1   = new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["knowledgebase"] ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ <a href='index.php?_m=knowledgebase&_a=insertquestion&parentcategoryid=0' id='blue' title='". $_SWIFT["language"]["addquestion"] ."'>". $_SWIFT["language"]["addquestion"] ."</a> | <a href='index.php?_m=knowledgebase&_a=insertcategory&parentcategoryid=0' id='blue' title='". $_SWIFT["language"]["addsubcat"] ."'>". $_SWIFT["language"]["addsubcat"] ."</a> ]", 'link' => "", 'icon' => "icon_list.gif", 'expandedIcon' => "icon_list.gif", 'expanded' => true));

	doKBCategoryTreeLoop(0, $categorydata, $node1);

	$menu->addItem($node1);
	
	// Create the presentation class
	$treeMenu = &new HTML_TreeMenu_DHTML($menu, array('images' => stripTrailingSlash($_SWIFT["themepath"]), 'defaultClass' => 'treeMenuDefault'));

	$interface->staffHeader($_SWIFT["language"]["knowledgebase"]." &gt; ".$_SWIFT["language"]["managecategories"], 4);

	$interface->staffNavBar('<a href="index.php?_m=knowledgebase&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a>', "", 4);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	// ======= FILTER =======
	echo '<div id="filtertscat" style="DISPLAY:none;">'.SWIFT_CRLF;
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["filter"]);

	$options = array();
	$index = 0;
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."templategroups` ORDER BY `title` ASC;");
	while ($dbCore->nextRecord())
	{
		$options[$index]["title"] = $dbCore->Record["title"];
		$options[$index]["value"] = $dbCore->Record["tgroupid"];
		$index++;
	}

	printSelectRow("filtertgroupid", $_SWIFT["language"]["filtertgroupid"], $_SWIFT["language"]["desc_filtertgroupid"], $options);
	printSubmitRow($_SWIFT["language"]["filter"]);
	printMainTableFooter();
	addHiddenField("_m", "knowledgebase");
	addHiddenField("_a", "managecategories");
	addHiddenField("filter", "1");
	printFormEnd();
	echo '<BR /></div>'.SWIFT_CRLF;	


	printMainTableHeader($_SWIFT["language"]["categorylist"]);
	// ======= TOOLBAR OPTIONS =======
	echo '<tr>'.SWIFT_CRLF;
	echo '<td class="toolbarrow" align="left" colspan="2" valign="middle" nowrap><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="5"><img src="'. $_SWIFT["themepath"] .'grip.gif" border="0" /></td><td class="toolbarrow"><span class="toolbartext">&nbsp;<a href="javascript:switchDisplay(\'filtertscat\');" id="white" title="'. $_SWIFT["language"]["filter"] .'">'. $_SWIFT["language"]["filter"] .'</a></span></td></tr></table></td>'.SWIFT_CRLF;
	echo '</tr>'.SWIFT_CRLF;
	// ======= TOOLBAR OPTIONS =======

	ob_start();
	?><div style="position:relative; width:100%; height:320px; background-color:#ffffff; overflow:auto;"><?
	$treeMenu->printMenu();
	?></div><?
	$contents = ob_get_contents();
	ob_end_clean();

	printDescRow($contents, "2", "defaultrow");

	printMainTableFooter();

	$interface->staffFooter();







/**
* ###############################################
* INSERT KB CATEGORY
* ###############################################
*/
} else if ($eventaction == "insertcategory") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);	

			$kbcategoryid = insertKnowledgebaseCategory($_POST["parentcategoryid"], $_POST["title"], $_POST["displayorder"], iif($_POST["categorytype"]==1, SWIFTPUBLIC, SWIFTPRIVATE), $_POST["tgroupid"]);

			printRedirect(sprintf($_SWIFT["language"]["kbcatinsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=knowledgebase&_a=managecategories&insertcat=".urlencode($_POST["title"]));
			exit;
		}		
	}

	$interface->staffHeader($_SWIFT["language"]["knowledgebase"]." &gt; ".$_SWIFT["language"]["insertcategory"], 4);

	$interface->staffNavBar('<a href="index.php?_m=knowledgebase&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a> &raquo; '.'<a href="index.php?_m=knowledgebase&_a=insertcategory" title="'.$_SWIFT["language"]["insertcategory"].'">'.$_SWIFT["language"]["insertcategory"].'</a>', "", 4);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderKnowledgebaseCatForm(INSERT);
	
	$template->assign("backurl", "index.php?_m=knowledgebase&_a=managecategories");

	$interface->staffFooter();



/**
* ###############################################
* EDIT KNOWLEDGEBASE CATEGORY
* ###############################################
*/
} else if ($eventaction == "editcategory") {
	$_kbcategory = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."kbcategories` WHERE `kbcategoryid` = '". intval($_REQUEST["kbcategoryid"]) ."';");
	if (empty($_kbcategory["kbcategoryid"]))
	{
		trigger_error($_SWIFT["language"]["invalidkbcat"], E_USER_ERROR);
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];			
		} else if ($_POST["parentcategoryid"] == $_kbcategory["kbcategoryid"]) {
			$errormessage = $_SWIFT["language"]["errornoparent"];				
		} else {
			$_POST["displayorder"] = iif(empty($_POST["displayorder"]), "1", $_POST["displayorder"]);	

			updateKnowledgebaseCategory($_kbcategory["kbcategoryid"], $_POST["parentcategoryid"], $_POST["title"], $_POST["displayorder"], iif($_POST["categorytype"]==1, SWIFTPUBLIC, SWIFTPRIVATE), $_POST["tgroupid"]);

			printRedirect(sprintf($_SWIFT["language"]["kbcatupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=knowledgebase&_a=managecategories&updatecat=".urlencode($_POST["title"]));
			exit;
		}		
	}

	$_POST = array_merge($_POST, $_kbcategory);

	$interface->staffHeader($_SWIFT["language"]["knowledgebase"]." &gt; ".$_SWIFT["language"]["editcategory"], 4);

	$interface->staffNavBar('<a href="index.php?_m=knowledgebase&_a=managecategories" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"].'</a> &raquo; '.'<a href="index.php?_m=knowledgebase&_a=editcategory&kbcategoryid='. intval($_kbcategory["kbcategoryid"]) .'" title="'.$_SWIFT["language"]["editcategory"].'">'.$_SWIFT["language"]["editcategory"].'</a>', "", 4);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderKnowledgebaseCatForm(EDIT);
	
	$template->assign("backurl", "index.php?_m=knowledgebase&_a=managecategories");

	$interface->staffFooter();

}
?>